<?php
    namespace Home\Controller;
    use Think\Controller;
    class JixiaokeshishezhiController extends Controller {
    
    public function _initialize()
    {
        header("Access-Control-Allow-Origin: *");  

        header("Access-Control-Allow-Credentials: true"); 
      
        header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS"); //允许的请求类型
        
        header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept"); 

    }

    public function get_list()
    {
        $page = I("get.page");
        $limit = I("get.limit");
		
		
        $data = M("comm.sys_dept_dict")->limit(($page-1)*$limit,$limit)->select();

        //科室类别
        $sys_dept_attr_dict_list = M("comm.sys_dept_attr_dict")->select();
        
        for ($i=0; $i < count($data); $i++) 
        { 
            $info = $data[$i];

            for ($j=0; $j < count($sys_dept_attr_dict_list); $j++) 
            { 

                if($info['dept_type'] == $sys_dept_attr_dict_list[$j]['id'])
                { 
                    $data[$i]['dept_type'] = $sys_dept_attr_dict_list[$j]['dept_type'];
                }
            }

        }
        $json["data"] = $data;
        $json["code"] = 0;
        $json["count"] = M("comm.sys_dept_dict")->count();
        $json["msg"] = "";
        
        $this->ajaxReturn($json,"json");
    }

    public function get_one()
    {
        $dept_code = I("post.dept_code");

        $info = M("comm.sys_dept_dict")->where("dept_code = '$dept_code'")->find();
        $info["dept_date"] = date("Y-m-d H:i:s",$info["dept_date"]);
        $json["data"]   = $info;
        $json["code"]   = 0;
        $json["msg"]    = "";

        $this->ajaxReturn($json,"json");
    }

    public function get_sys_dept_attr_dict()
    {
        $list = M("comm.sys_dept_attr_dict")->select();
        $json["data"]   = $list;
        $json["code"]   = 0;
        $json["msg"]    = "";
        $this->ajaxReturn($json,"json");
    }

    public function get_sys_lcdept_attr_dict()
    {
        $list = M("comm.sys_lcdept_attr_dict")->select();
        $json["data"]   = $list;
        $json["code"]   = 0;
        $json["msg"]    = "";
        $this->ajaxReturn($json,"json");
    }

    public function get_dept_dict()
    {
        $list = M("hisdata.dept_dict")->select();
        $json["data"]   = $list;
        $json["code"]   = 0;
        $json["msg"]    = "";
        $this->ajaxReturn($json,"json");
    }

    public function save_one()
    {
        $data = I("post.");
        $dept_code = $data["dept_code"];

        $data['dept_date'] = strtotime(date('Y-m'),time());

        $result = M("comm.sys_dept_dict")->where("dept_code = '$dept_code'")->save($data);

        if($result !== false)
        {
            $json["code"] = 0;
            $json["msg"] = "修改成功";
        }
        else
        {
            $json["code"] = -1;
            $json["msg"] = "修改失败";
        }
 
        $this->ajaxReturn($json,"json");
    }

    public function add_one()
    {
        $data = I("post.");
        $result = M("comm.sys_dept_dict")->add($data);
        if($result !== false)
        {
            $json["code"] = 0;
            $json["msg"] = "添加成功";
        }
        else
        {
            $json["code"] = -1;
            $json["msg"] = "添加失败";
        }
 
        $this->ajaxReturn($json,"json");
    }
   }